package com.fjeld.middleware.activemq;

import com.fjeld.swingx.ColorConstants;
import org.apache.log4j.Logger;

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;

import static com.fjeld.middleware.util.serverstatus.ServerStatus.Unknown;
import static com.fjeld.middleware.util.serverstatus.ServerStatus.Unresponsive;

/**
 * Created by IntelliJ IDEA.
 * User: fjeld
 * Date: 28/12/2011
 * Time: 16:25
 * <p/>
 * Highlights the cell if the a heartbeat has been missed
 */
public class HeartbeatCellRenderer extends JLabel implements TableCellRenderer
{
    public HeartbeatCellRenderer()
    {
        setBackground(ColorConstants.BLUE_HIGHLIGHT);
        setOpaque(false);
        setForeground(ColorConstants.TEXT_NORMAL);
    }

    public Component getTableCellRendererComponent(JTable jTable, Object value,
                                                   boolean isSelected, boolean hasFocus, int row, int column)
    {
        String text = null != value ? value.toString() : "";
        setText(text);

        // If this is the status column then manage highlights for special server statuses
        boolean needHighlight = Unresponsive.toString().equals(text) || Unknown.toString().equals(text);
        setOpaque(needHighlight);

        if (needHighlight)
        {
            setForeground(ColorConstants.BLACK);
        } else
        {
            setForeground(ColorConstants.TEXT_NORMAL);
        }

        return this;
    }
}
